﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 赵国伟
  //文件名：ConsultManage.cs
  //文件功能描述：咨询管理
  //创建人：赵国伟
  //生成时间：2012/03/17 16:15:00
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Shopping.Common;
using Shopping.Model;
using Shopping.DAL;

namespace Shopping.BLL
{
    public class ConsultManage
    {
        private readonly DALGoodsConsults dalConsult = new DALGoodsConsults();
        public ReturnValue Add(GoodsConsults eval)
        {
            ReturnValue ret = new ReturnValue();
            if (dalConsult.Add(eval) > 0)
            {
                ret.Message = "保存成功";
                ret.Result = ResultType.Success;

            }
            return ret;
        }

        #region "分页查询指定的商品咨询信息； by 赵国伟 2012-04-14"
        /// <summary>
        /// 分页查询指定的商品咨询信息；
        /// </summary>
        /// <param name="keyWords"></param>
        /// <param name="GoodsId"></param>
        /// <param name="status"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public List<GoodsConsultInfo> QueryListAsPage(string keyWords, int GoodsId, string status, AsPage p)
        {
            string cond = "1=1 ";
            if (!string.IsNullOrEmpty(keyWords))
                cond += " and ConsultContent like '%" + keyWords + "%' ";
            if (GoodsId > 0)
                cond += " and GoodsID=" + GoodsId;
            if (status == GoodsConsultStatus.未回复.ToString())
                cond += " and ReplyContent ='' ";
            else if (status == GoodsConsultStatus.已回复.ToString())
                cond += " and ReplyContent <>'' ";

            string orderby = "GoodsConsultID desc ";

            List<GoodsConsultInfo> listOfTarget = null;
            List<GoodsConsults> listOfSource = dalConsult.QueryListAsPage(cond, orderby, p.CurPage, p.PageSize, out p.TotalCount);
            if (listOfSource != null)
            {
                listOfTarget = new List<GoodsConsultInfo>();

                Dictionary<int, string> dictUsers = BLLHelper.GenerateToHash<Users>("UserID in(Select UserID From GoodsConsults where " + cond + ")", UsersEnum.UserName);
                Dictionary<int, string> dictGoods = BLLHelper.GenerateToHash<Goods>("GoodsID in(Select GoodsID From GoodsConsults where " + cond + ")", GoodsEnum.GoodsName);

                foreach (GoodsConsults source in listOfSource)
                {
                    GoodsConsultInfo target = new GoodsConsultInfo();
                    target.Consult = source;

                    if (dictUsers.ContainsKey(source.UserID))
                        target.UserName = dictUsers[source.UserID];
                    else
                        target.UserName = "未知";

                    if (dictGoods.ContainsKey(source.GoodsID))
                        target.GoodsName = dictGoods[source.GoodsID];
                    else
                        target.GoodsName = "";
                    listOfTarget.Add(target);
                }
            }
            return listOfTarget;
        }
        #endregion

        #region "删除一条指定的咨询信息； by 赵国伟 2012-04-14"
        /// <summary>
        /// 删除一条指定的咨询信息；
        /// </summary>
        /// <param name="GoodsConsultID"></param>
        /// <returns></returns>
        public int Delete(int GoodsConsultID)
        {
            return dalConsult.Delete(GoodsConsultID);
        }
        #endregion

        #region "咨询回复； by 赵国伟 2012-04-14"
        /// <summary>
        /// 咨询回复；
        /// </summary>
        /// <param name="GoodsConsultID"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public int Reply(int GoodsConsultID, string content)
        {
            GoodsConsults o = dalConsult.GetModel(GoodsConsultID);
            o.ReplyContent = content;
            o.ReplyDate = DateTime.Now;
            return dalConsult.Update(o);
        }
        #endregion

        #region "分页查询用户咨询问题； by 党明 2012-04-14"
        /// <summary>
        /// 分页查询指定的商品咨询信息；
        /// </summary>
        /// <param name="keyWords"></param>
        /// <param name="GoodsId"></param>
        /// <param name="status"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public List<GoodsConsultInfo> QueryListAsPageByUser(string keyWords, int userId, string status, AsPage p)
        {
            string cond = "1=1 ";
            if (!string.IsNullOrEmpty(keyWords))
                cond += " and ConsultContent like '%" + keyWords + "%' ";
            if (userId > 0)
                cond += " and UserID=" + userId;
            if (status == GoodsConsultStatus.未回复.ToString())
                cond += " and ReplyContent ='' ";
            else if (status == GoodsConsultStatus.已回复.ToString())
                cond += " and ReplyContent <>'' ";

            string orderby = "GoodsConsultID desc ";

            List<GoodsConsultInfo> listOfTarget = null;
            List<GoodsConsults> listOfSource = dalConsult.QueryListAsPage(cond, orderby, p.CurPage, p.PageSize, out p.TotalCount);
            if (listOfSource != null)
            {
                listOfTarget = new List<GoodsConsultInfo>();

                Dictionary<int, string> dictUsers = BLLHelper.GenerateToHash<Users>("UserID in(Select UserID From GoodsConsults where " + cond + ")", UsersEnum.UserName);
                Dictionary<int, string> dictGoods = BLLHelper.GenerateToHash<Goods>("GoodsID in(Select GoodsID From GoodsConsults where " + cond + ")", GoodsEnum.GoodsName);

                foreach (GoodsConsults source in listOfSource)
                {
                    GoodsConsultInfo target = new GoodsConsultInfo();
                    target.Consult = source;

                    if (dictUsers.ContainsKey(source.UserID))
                        target.UserName = dictUsers[source.UserID];
                    else
                        target.UserName = "未知";

                    if (dictGoods.ContainsKey(source.GoodsID))
                        target.GoodsName = dictGoods[source.GoodsID];
                    else
                        target.GoodsName = "";
                    listOfTarget.Add(target);
                }
            }
            return listOfTarget;
        }
        #endregion
    }
}
